package remoteRpc

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/rpc/proto"
	"google.golang.org/grpc"
	"log"
	"sync"
)

var (
	postRpc       *RemotePostRpc
	oncePostCache sync.Once
)

func GetPostRpc() *RemotePostRpc {
	if postRpc == nil {
		baseLog.Errorf("smsRpc instance is %v", postRpc)
	}
	return postRpc
}

type RemotePostRpc struct {
	lock                 *sync.RWMutex
	PostRpcServiceClient proto.PostRpcServiceClient
}

func InitPostRpc(rpcAddr string) (*RemotePostRpc, error) {
	oncePostCache.Do(func() {
		conn, err := grpc.Dial(rpcAddr, grpc.WithInsecure())
		if err != nil {
			log.Fatalf("rpc did not connect: %v", err)
		}
		postRpc = &RemotePostRpc{
			PostRpcServiceClient: proto.NewPostRpcServiceClient(conn),
		}
	})
	return postRpc, nil
}
